﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.SqlServer.Repositories.Readers
{
	using System.Data.SqlClient;
	using Falcon.Core.Domain;
	using Falcon.Core.Domain.Enums;

	public class UserContactReader : IReader<UserContact>
	{
		/// <summary>
		/// Populates the specified reader.
		/// </summary>
		/// <param name="reader">The reader.</param>
		/// <param name="entity">The entity.</param>
		public void Populate(SqlDataReader reader, UserContact entity)
		{
			entity.Id = reader.GetInt32(0);
			entity.Status = (UserContactStatus)reader.GetInt32(1);
			entity.Created = reader.GetDateTime(2);
			entity.Updated = reader.GetDateTime(3);

			entity.User = new User
			{
				Id = reader.GetInt32(4),
				Name = reader.GetString(5),
				Slug = reader.GetString(6),
				Reputation = reader.GetInt32(7),
				Profile = new UserProfile { Title = reader.GetString(8) }
			};

			if (!reader.IsDBNull(9))
			{
				entity.User.Profile.Avatar = new UserAvatar
				{
					FileName = reader.GetString(9),
					FileDiskName = reader.GetString(10),
					FilePath = reader.GetString(11),
					FileType = reader.GetString(12)
				};
			}

			entity.Contact = new User
			{
				Id = reader.GetInt32(13),
				Name = reader.GetString(14),
				Slug = reader.GetString(15),
				Reputation = reader.GetInt32(16),
				Profile = new UserProfile { Title = reader.GetString(17) }
			};

			if (!reader.IsDBNull(18))
			{
				entity.Contact.Profile.Avatar = new UserAvatar
				{
					FileName = reader.GetString(18),
					FileDiskName = reader.GetString(19),
					FilePath = reader.GetString(20),
					FileType = reader.GetString(21)
				};
			}
		}
	}
}